package com.sgb.kjwl.view.ui.adapter.self;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;

import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ItemSelfLinkComListBinding;
import com.sgb.kjwl.model.entity.businessmanagement.CompanyEntity;
import com.sgb.kjwl.view.ui.activity.self.EnterpriseInformationActivity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.List;

/**
 * 我的信息-新企业列表
 * 2022/1/5
 * 描述:新增设置主企业以及展示加入企业状态
 */
public class SelfEnterprisesListAdapter extends BaseRecycleAdapter<CompanyEntity> {

    private RequestManager requestManager;

    public SelfEnterprisesListAdapter(Context context, List<CompanyEntity> data, RequestManager requestManager) {
        super(context, R.layout.item_self_link_com_list, data);
        this.requestManager = requestManager;
    }

    @Override
    public void convert(ItemViewHolder holder, CompanyEntity companyEntity) {
        holder.setBinding(BR.viewModel, companyEntity);
        ItemSelfLinkComListBinding binding = (ItemSelfLinkComListBinding) holder.getmBinding();
        RequestOptions options = new RequestOptions();
        switch (companyEntity.getType()){
            case 1:
                options.placeholder(R.mipmap.icon_company);
                options.error(R.mipmap.icon_company);
                options.transform(new RoundedCorners(5));
                break;
            case 2:
                options.placeholder(R.drawable.icon_team_default);
                options.error(R.drawable.icon_team_default);
                options.transform(new RoundedCorners(5));
                break;
            case 3:
                options.placeholder(R.drawable.icon_organization_default);
                options.error(R.drawable.icon_organization_default);
                options.transform(new RoundedCorners(5));
                break;
        }
        requestManager.load(companyEntity.getCompLogo()).apply(options).into(binding.ivCompanyLogo);
        binding.tvCompanyName.setText(companyEntity.getCompName());
        String realName = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.REAL_NAME, "");
        if (TextUtils.isEmpty(realName)) {
            binding.tvSelfName.setVisibility(View.GONE);
        } else {
            binding.tvSelfName.setVisibility(View.VISIBLE);
            binding.tvSelfName.setText(realName);
        }
        binding.tvIdentity.setText(companyEntity.getRoleName());
        if (TextUtils.equals(companyEntity.getDefaultCompany(), "1")) {
            binding.ivIsMainCom.setVisibility(View.VISIBLE);
        } else {
            binding.ivIsMainCom.setVisibility(View.GONE);
        }
        switch (companyEntity.getStatus()) {
            case 1: //审核中
                binding.tvLinkState.setText("审核中");
                binding.tvLinkState.setTextColor(ContextCompat.getColor(context, R.color.color_5792FD));
                binding.layoutConfig.setVisibility(View.GONE);
                break;
            case 2: //已加入
                binding.tvLinkState.setText("已加入");
                binding.tvLinkState.setTextColor(ContextCompat.getColor(context, R.color.color_06C764));
                if (TextUtils.equals(companyEntity.getDefaultCompany(), "1") || companyEntity.getType() != 1) {
                    binding.layoutConfig.setVisibility(View.GONE);
                } else {
                    binding.layoutConfig.setVisibility(View.VISIBLE);
                }
                break;
            case 3: //未通过
                binding.tvLinkState.setText("未通过");
                binding.tvLinkState.setTextColor(ContextCompat.getColor(context, R.color.color_EF4033));
                binding.layoutConfig.setVisibility(View.GONE);
                break;
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到企业详情界面（带公司具体参数）
                if (companyEntity.getStatus() == 2)
                    EnterpriseInformationActivity.start(context, companyEntity);
            }
        });
        binding.btnConfig.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onDefaultConfig(companyEntity);
            }
        });
    }

    private OnConfigDefaultCompanyListener listener;

    public interface OnConfigDefaultCompanyListener {
        void onDefaultConfig(CompanyEntity companyEntity);
    }

    public void setListener(OnConfigDefaultCompanyListener listener) {
        this.listener = listener;
    }
}
